<template>
  <div>
  <el-row :gutter="10">
    <el-col :span="5">
     <el-card  shadow="never" :body-style="contentViewHeight">
      <el-tag
        closable
        size="small"
        style="margin-bottom:5px"
        v-if="selectMonitorVideoDetectionMainName"
        :disable-transitions="false"
        @close="handleNodeClose">
        {{selectMonitorVideoDetectionMainName}}
      </el-tag>
      <el-row :gutter="5">
        <el-col :span="20">
          <el-input
            placeholder="输入关键字进行过滤"
            size="small"
            v-model="filterText">
          </el-input>
        </el-col>
        <el-col :span="4">
            <el-button type="primary" @click="addTreeNode" size="small" icon="el-icon-plus" circle></el-button>
        </el-col>
      </el-row>
      <el-tree
        class="filter-tree"
        :data="monitorVideoDetectionMainTreeData"
        :props="{
              value: 'id',             // ID字段名
              label: 'name',         // 显示名称
              children: 'children'    // 子级字段名
            }"
        default-expand-all
        :filter-node-method="filterNode"
        :expand-on-click-node="false"
        @node-click="handleNodeClick"
        ref="monitorVideoDetectionMainTree">
           <span class="custom-tree-node" slot-scope="{ node, data}">
            <span>{{ node.label }}</span>
            <span>
               <el-button type="text" class="tree-item-button" icon="el-icon-plus" @click="() => addChildTreeNode(data)">
               </el-button>
                <el-button type="text" class="tree-item-button" icon="el-icon-edit-outline" @click="() => editTreeNode(data)">
               </el-button>
                <el-button type="text" class="tree-item-button" icon="el-icon-delete" @click="() => delTreeNode(data)">
               </el-button>
            </span>
          </span>
      </el-tree>
      </el-card>
    </el-col>
    <el-col :span="19">
    <el-card  shadow="never" :body-style="contentViewHeight">

      <span v-if="videoUrl[1] !== undefined" v-for="(item,index) in videoUrl">
        <!-- <div> -->
              <object type='application/x-vlc-plugin' pluginspage="http://www.videolan.org/" id='vlc' events='false' width="50%" height="400px">
                     <param name='mrl' :value= "item"/>
                     <param name='volume' value='50' />
                     <param name='autoplay' value='true' />
                     <param name='loop' value='false' />
                     <param name='fullscreen' value='false' />
                     <param name='controls' value='false' />
                 </object>
        <!-- </div> -->
      </span>
      <div v-if="videoUrl[1] === undefined" v-for="(item,index) in videoUrl" :gutter="100">
            <object type='application/x-vlc-plugin' pluginspage="http://www.videolan.org/" id='vlc' events='false' width="100%" height="600px">
                   <param name='mrl' :value= "item"/>
                   <param name='volume' value='50' />
                   <param name='autoplay' value='true' />
                   <param name='loop' value='false' />
                   <param name='fullscreen' value='false' />
                   <param name='controls' value='false' />
               </object>
      </div>
    <el-pagination
      v-if="videoUrl[1] !== undefined"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageNo"
      :page-sizes="[4]"
      :page-size="pageSize"
      :total="total"
      background
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    </el-card>
    </el-col>
</el-row>
        <!-- 弹窗, 新增 / 修改 -->
    <MonitorVideoDetectionChildForm  ref="monitorVideoDetectionChildForm" @refreshDataList="refreshList"></MonitorVideoDetectionChildForm>
    <MonitorVideoDetectionMainForm  ref="monitorVideoDetectionMainForm" @refreshTree="refreshTree"></MonitorVideoDetectionMainForm>

  </div>
</template>

<script>
  import MonitorVideoDetectionChildForm from './MonitorVideoDetectionChildForm'
  import MonitorVideoDetectionMainForm from './MonitorVideoDetectionMainForm'
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  export default {
    data () {
      return {
        videoUrl: [],
        searchForm: {
          contact: {
            id: ''
          }
        },
        filterText: '',
        monitorVideoDetectionMainTreeData: [],
        selectMonitorVideoDetectionMainName: '',
        dataList: [],
        pageNo: 1,
        pageSize: 4,
        total: 0,
        orderBy: '',
        dataListSelections: [],
        isSearchCollapse: false,
        isImportCollapse: false,
        loading: false
      }
    },
    components: {
      SelectTree,
      MonitorVideoDetectionChildForm,
      MonitorVideoDetectionMainForm
    },
    activated () {
      this.refreshTree()
      this.refreshList()
    },
    computed: {
      contentViewHeight () {
        let height = this.$store.state.common.documentClientHeight - 122
        return {minHeight: height + 'px'}
      }
    },
    watch: {
      filterText (val) {
        this.$refs.monitorVideoDetectionMainTree.filter(val)
      }
    },
    methods: {
      filterNode (value, data) {
        if (!value) return true
        return data.name.indexOf(value) !== -1
      },
      refreshTree () {
        this.$http({
          url: `/monitor/videodetection/monitorVideoDetectionMain/treeData`,
          method: 'get'
        }).then(({data}) => {
          this.monitorVideoDetectionMainTreeData = data.treeData
        })
      },
      handleNodeClick (data) {
        this.searchForm.contact.id = data.id
        this.selectMonitorVideoDetectionMainName = '已选: ' + data.name
        this.refreshList()
      },
      handleNodeClose () {
        this.searchForm.contact.id = ''
        this.selectMonitorVideoDetectionMainName = ''
        this.refreshList()
      },
      addChildTreeNode (node) {
        this.$refs.monitorVideoDetectionMainForm.init('addChild', {id: '', parent: {id: node.id, name: node.name}})
      },
       // 新增
      addTreeNode () {
        this.$refs.monitorVideoDetectionMainForm.init('add', {id: '', parent: {id: '', name: ''}})
      },
      // 修改
      editTreeNode (data) {
        this.$refs.monitorVideoDetectionMainForm.init('edit', {id: data.id, parent: {id: '', name: ''}})
      },
      delTreeNode (data) {
        this.$confirm(`确定删除所选项吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.$http({
            url: '/monitor/videodetection/monitorVideoDetectionMain/delete',
            method: 'delete',
            params: {'id': data.id}
          }).then(({data}) => {
            if (data && data.success) {
              this.$message.success(data.msg)
              this.loading = false
              this.refreshTree()
              this.refreshList()
            }
          })
        })
      },
      // 获取数据列表  rtsp://admin:hljjl12345@10.4.204.101:554/Streaming/Channels/101?transportmode=unicast
      refreshList () {
        this.loading = true
        this.$http({
          url: '/monitor/videodetection/monitorVideoDetectionChild/list',
          method: 'get',
          params: {
            'pageNo': this.pageNo,
            'pageSize': this.pageSize,
            'orderBy': this.orderBy,
            ...this.searchForm
          }
        }).then(({data}) => {
          if (data && data.success) {
            this.dataList = data.page.list
            this.total = data.page.count
            this.videoUrl = []
            for (let var1 in this.dataList) {
              var Durl = 'rtsp://' + this.dataList[var1].monitorUser + ':' + this.dataList[var1].monitorPassword + '@' + this.dataList[var1].monitorIpAdress + ':' + this.dataList[var1].monitorEquipmentIp + '/Streaming/Channels/101?transportmode=unicast'
              this.videoUrl.push(Durl)
            }
            console.log(this.videoUrl)
            this.loading = false
          }
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageNo = 1
        this.refreshList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageNo = val
        this.refreshList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },

    // 排序
      sortChangeHandle (obj) {
        if (obj.order === 'ascending') {
          this.orderBy = obj.prop + ' asc'
        } else if (obj.order === 'descending') {
          this.orderBy = obj.prop + ' desc'
        } else {
          this.orderBy = ''
        }
        this.refreshList()
      },
      // 新增
      add () {
        this.$refs.monitorVideoDetectionChildForm.init('add', '')
      },
      // 修改
      edit (id) {
        id = id || this.dataListSelections.map(item => {
          return item.id
        })[0]
        this.$refs.monitorVideoDetectionChildForm.init('edit', id)
      },
      // 查看
      view (id) {
        this.$refs.monitorVideoDetectionChildForm.init('view', id)
      },
      // 删除
      del (id) {
        let ids = id || this.dataListSelections.map(item => {
          return item.id
        }).join(',')
        this.$confirm(`确定删除所选项吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.$http({
            url: '/monitor/videodetection/monitorVideoDetectionChild/delete',
            method: 'delete',
            params: {'ids': ids}
          }).then(({data}) => {
            if (data && data.success) {
              this.$message.success(data.msg)
              this.refreshList()
            }
            this.loading = false
          })
        })
      },
      // 导入成功
      uploadSuccess (res, file) {
        if (res.success) {
          this.$message.success({dangerouslyUseHTMLString: true,
            message: res.msg})
        } else {
          this.$message.error(res.msg)
        }
      },
      // 下载模板
      downloadTpl () {
        this.$utils.download('/monitor/videodetection/monitorVideoDetectionChild/import/template')
      },
      exportExcel () {
        this.$utils.download('/monitor/videodetection/monitorVideoDetectionChild/export')
      },
      resetSearch () {
        this.$refs.searchForm.resetFields()
        this.selectMonitorVideoDetectionMainName = ''
        this.refreshList()
      }
    }
  }
</script>
